import {request,GetQueryString} from "./utils/request";
import {weixinopenid} from "./utils/apis";
if(sessionStorage.openid&&sessionStorage.openid.length>10){
}
else  if(navigator.userAgent.indexOf("MicroMessenger/")==-1||GetQueryString('AttractionsPrice')==0){
    localStorage.AID = GetQueryString('AID');
    localStorage.ScenicName = GetQueryString('ScenicName');
    localStorage.AttractionsPrice = GetQueryString('AttractionsPrice');
}
else if(GetQueryString('code')){
    // localStorage.code = GetQueryString('code');
    getWeixinopenid();
    
}else{
    localStorage.AID = GetQueryString('AID');
    localStorage.ScenicName = GetQueryString('ScenicName');
    localStorage.AttractionsPrice = GetQueryString('AttractionsPrice');
    var appid  = 'wxb3209df4b8908245',
    redirect_uri =encodeURI(location.href);//用户授权后的回调链接 只能是域名方式
    //微信公众号 登录验证，需要用户自己授权 
    window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;
}

function getWeixinopenid(){
    var code = GetQueryString('code');
    if(!(code&&code.length>0))return;
    //获取openid
    request(weixinopenid+"?code="+code, {
        code
    }, (result)=>{
        var openid = result.split("\n")[0].replace(/\s/g,'');
        sessionStorage.openid = openid;
        //  history.go(-2);////董文强修改
    }, (result)=>{
    		alert("获取openID失败");
        // console.log('getWeixinopenid:error')
    },"POST")
}